package com.weetop.aklyscan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.appConfig.MyApplication;
import com.weetop.aklyscan.base.BaseActivity;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.bean.RecordListBean;
import com.weetop.aklyscan.presenter.RecordPresent;
import com.weetop.aklyscan.view.RecordView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InRecordListActivity extends BaseActivity<RecordPresent> implements RecordView, CommonRecyclerAdapter.OnItemClickListener {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.search_type)
    TextView search_type;
    @BindView(R.id.ll1)
    LinearLayout ll1;
    @BindView(R.id.number)
    TextView number;
    private CommonRecyclerAdapter<RecordListBean> adapter;
    private List<RecordListBean> list = new ArrayList<>();
    private String order_type, order_sn;

    @Override
    protected RecordPresent createPresenter() {
        return new RecordPresent(this, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_record_list);
        ButterKnife.bind(this);
        order_type = getIntent().getStringExtra("order_type");
        order_sn = getIntent().getStringExtra("order_sn");
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new CommonRecyclerAdapter<RecordListBean>(mActivity, R.layout.item_record_list_1, list) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, RecordListBean item, int position) {
                helper.setText(R.id.tv_xuhao, position + 1 + "");
                helper.setText(R.id.tv_num, item.getTotal_num());
                helper.setText(R.id.tv_num_1, order_sn);
                helper.setText(R.id.tv_time, item.getRk_time());
                helper.setText(R.id.tv_huojia, item.getApply_user_name());
            }
        };
        search_type.setTag(order_type);
        switch (order_type) {
            case "1":
                search_type.setText("产品采购");
                break;
            case "2":
                search_type.setText("退货处理");
                search_type.setEnabled(false);
                break;
            case "3":
                search_type.setText("配件采购");
                break;
            case "4":
                search_type.setText("配件采购");
                ll1.setVisibility(View.GONE);
                break;
        }
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        mPresenter.Into27(((String) search_type.getTag()), order_sn);
    }

    @OnClick({R.id.iv_back, R.id.search_type, R.id.chaxun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.search_type:
                XPopup.get(InRecordListActivity.this).asCenterList("请选择类型", new String[]{"产品采购", "配件采购"},
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text1) {
                                search_type.setText(text1);
                                if (position == 0) {
                                    search_type.setTag("1");
                                    order_type = "1";
//                                    order_sn = "CPCG202108210002";
                                } else {
                                    search_type.setTag("3");
                                    order_type = "3";
//                                    order_sn = "PJCG202108200001";
                                }
                            }
                        })
                        .show();
                break;
            case R.id.chaxun:
                mPresenter.Into27(((String) search_type.getTag()), order_sn);
                break;
        }
    }

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
        if (MyApplication.getPort().equals("1")) {
            Intent intent;
            if (order_type.equals("3") || order_type.equals("4")) {
                intent = new Intent(InRecordListActivity.this, InRecordActivity1.class);
            } else {
                intent = new Intent(InRecordListActivity.this, InRecordActivity.class);
            }
            intent.putExtra("order_type", order_type);
            intent.putExtra("order_sn", order_sn);
            intent.putExtra("time", list.get(i).getRk_time());
            startActivity(intent);
        } else {
            Intent intent = new Intent(InRecordListActivity.this, InRecordActivity1.class);
            intent.putExtra("order_type", order_type);
            intent.putExtra("order_sn", order_sn);
            intent.putExtra("time", list.get(i).getRk_time());
            startActivity(intent);
        }
    }

    @Override
    public void getInto27Success(BaseModel<List<RecordListBean>> o) {
        List<RecordListBean> listBeans = o.getData();
        int total = 0;
        for (int i = 0; i < listBeans.size(); i++) {
            total += Integer.parseInt(listBeans.get(i).getTotal_num());
        }
        number.setText(total + "");
        list.clear();
        adapter.clear();
        list.addAll(listBeans);
        adapter.addAll(listBeans);
    }
}
